'''
Created on Jan 31, 2012

@author: alebalbin

This compares two lrpath clustering outputs
identifies what is unique to the study case.
'''
import networkx as nx
from networkx.algorithms import bipartite
from collections import defaultdict

def read_files(file1,file2):
    '''
    '''
    if1=open(file1)
    if2=open(file2)
    d1=set()
    d2=set()
    data=False
    for l in if1:
        f=l.strip('\n').split('\t')
        if not data and f[0]=="EWEIGHT":
            data=True
            continue
        if data and float(f[4]) < 0 and float(f[5]) <0:
            d1.add(f[1])
    if1.close()

    data=False
    for l in if2:
        f=l.strip('\n').split('\t')
        if not data and f[0]=="EWEIGHT":
            data=True
            continue
        '''
        if data:
            d2.add(f[1])
        '''
        if data and float(f[4]) < 0 and float(f[5]) <0 and \
           float(f[5]) < 0 and float(f[6]) <0:
            d2.add(f[1])
        
    if2.close()
    
    diff = d1.difference(d2)
    
    ### Writting a f1 file to next analysis
    if1=open(file1)
    of=open(file1+'_filtered','w')
    d3=set()
    data=False
    for l in if1:
        f=l.strip('\n').split('\t')
        if not data and f[0]=="EWEIGHT":
            of.write(l)
            data=True
            continue
        if data and (f[1] in diff):
            print l
            of.write(l)
    if1.close()
    of.close()

    #print diff
    #print d1.intersection(d2)
    return diff

def read_concepts(file,unique_concepts):
    if1=open(file)
    concepts=set()
    genes=defaultdict(set)
    netG = nx.Graph()
    if1.next()
    for l in if1:
        f=l.strip('\n').split('\t')
        name = f[0]+' | '+f[1]
        #print name
        if name in unique_concepts:
            concepts.add(name)
            gc = f[8].split(',')
            for g in gc:
                g=g.strip()
                netG.add_edge(name,g)
                genes[name].add(g)
    if1.close()
    return netG,concepts,genes
'''    
# Filter out with respect to vcap and prostate cell lines control
file1='/Users/alebalbin/Desktop/Dropbox/KRAS/LCK/enrichment_analysis/H358_H441_analysis/H441-H358-GO-KEGG-cluster690742208.cdt'
file2='/Users/alebalbin/Desktop/Dropbox/KRAS/LCK/enrichment_analysis/control_vs_LCKKnockDown_cluster522320554.cdt'
file3='/Users/alebalbin/Desktop/Dropbox/KRAS/LCK/enrichment_analysis/H358_H441_analysis/H358_GO-KEGG_download817130239.txt'
ofile='/Users/alebalbin/Desktop/Dropbox/KRAS/LCK/enrichment_analysis/H358_H441_analysis/H358_GO-KEGG_notInControl-download817130239.tsv'
'''

'''
# Filter out with respect to lung cell lines controls transfected with siMET
file2='/Users/alebalbin/Desktop/Dropbox/KRAS/LCK/enrichment_analysis/H358_H441_analysis/H358_H441_MET_2_cluster860323551.cdt'
file1='/Users/alebalbin/Desktop/Dropbox/KRAS/LCK/enrichment_analysis/H358_H441_analysis/H441-H358-GO-KEGG-cluster690742208.cdt'
file3='/Users/alebalbin/Desktop/Dropbox/KRAS/LCK/enrichment_analysis/H358_H441_analysis/H358_GO-KEGG_download817130239.txt'
ofile='/Users/alebalbin/Desktop/Dropbox/KRAS/LCK/enrichment_analysis/H358_H441_analysis/H358_GO-KEGG_notInMET-download817130239.tsv'
'''

'''
# Summarize concepts not in prostate control neither in siMET transfection.
file1='/Users/alebalbin/Desktop/Dropbox/KRAS/LCK/enrichment_analysis/H358_H441_analysis/H441-H358-GO-KEGG-cluster690742208.cdt_filtered'
file2='/Users/alebalbin/Desktop/Dropbox/KRAS/LCK/enrichment_analysis/control_vs_LCKKnockDown_cluster522320554.cdt'
file3='/Users/alebalbin/Desktop/Dropbox/KRAS/LCK/enrichment_analysis/H358_H441_analysis/H358_GO-KEGG_download817130239.txt'
ofile='/Users/alebalbin/Desktop/Dropbox/KRAS/LCK/enrichment_analysis/H358_H441_analysis/H358_GO-KEGG_notInControl_notsiMET-download817130239.2.tsv'


unique_concepts = read_files(file1,file2)

netG,concepts,genes=read_concepts(file3,unique_concepts)
print len(unique_concepts)
B = bipartite.weighted_projected_graph(netG, concepts, ratio=False)
of=open(ofile,'w')
hd=['concept A','concept B','Number of common Genes','Unique Genes A','Unique Genes B','common Genes']
of.write(",".join(map(str,hd)).replace(',','\t')+'\n')
for e in B.edges(data=True):
    uA = ",".join(list(genes[e[0]].difference(genes[e[1]]))).replace(',',';')
    uB = ",".join(list(genes[e[1]].difference(genes[e[0]]))).replace(',',';')
    cAB = ",".join(list(genes[e[1]].intersection(genes[e[0]]))).replace(',',';')
    ol=[e[0].replace(',','&'),e[1].replace(',','&'),e[2]['weight'],uA,uB,cAB]#+list(uA)+list(uB)+list(cAB)
    print ol
    of.write(",".join(map(str,ol)).replace(',','\t')+'\n')

of.close()
'''
file = '/Users/alebalbin/Desktop/Dropbox/KRAS/LCK/enrichment_analysis/H358_H441_analysis/H358_GO-KEGG_notInControl_notsiMET_genesInApoptosis.txt'
if1 = open(file)
of0 = '/Users/alebalbin/Desktop/Dropbox/KRAS/LCK/enrichment_analysis/H358_H441_analysis/H358_GO-KEGG_notInControl_notsiMET_genesInApoptosis.2.txt'
of = open(of0,'w')

for l in if1:
    f= l.strip('\n\r').split('\t')
    ol0=[f[0],f[3]]
    of.write(",".join(map(str,ol0)).replace(';','\t')+'\n')
    ol0=[f[1],f[4]]
    of.write(",".join(map(str,ol0)).replace(';','\t')+'\n')
    ol0=['common',f[5]]
    of.write(",".join(map(str,ol0)).replace(';','\t')+'\n')
    


    